package main

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"time"
)

type Student struct {
	Name       string `gorm:"column:name;primaryKey"`
	Province   string
	City       string
	Address    string `gorm:"column:addr"`
	Score      float32
	Enrollment time.Time
}

func (Student) TableName() string {
	return "student" //表名，默认表名是students
}

func create(db *gorm.DB) {
	student := Student{
		Name:       "dingwenze",
		Province:   "湖北",
		City:       "武汉",
		Address:    "棒槌区",
		Score:      0,
		Enrollment: time.Now(),
	}
	tx := db.Create(&student)
	fmt.Println(tx.Statement.Dest)
}

func query(db *gorm.DB) {
	var students []Student
	db.Where("score>?", 0).Find(&students)
	for _, ele := range students {
		fmt.Printf(ele.Name)
	}
}

func update(db *gorm.DB) {
	student := Student{Name: "ffrssw"}
	db = db.Model(&student).Where("city=?", "大同").
		Updates(map[string]interface{}{"score": 100})
}

func delete(db *gorm.DB) {
	var student = Student{
		Name: "dingwenze",
	}
	db.Delete(&student)

}

func main() {
	dsn := "admin:yicai127@tcp(192.168.133.2:3306)/test?charset=utf8mb4&parseTime=True"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}

	create(db)
	//query(db)
	//delete(db)
}
